<?php

namespace app\api\controller;

use app\common\controller\Api;
use TencentCloud\Tmt\V20180321\TmtClient;
use TencentCloud\Common\Credential;
use TencentCloud\Tmt\V20180321\Models\TextTranslateRequest;
use think\Config;
use think\Db;
use think\Env;
use think\View;

class Buildhtml extends Api
{
    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';
    private $view;
    private $lanArr;//语言信息
    private $moduleArr;//模块信息
    private $defaultPageSize = 10;//默认分页大小

//    private $prodcutCateIndex = [//产品二级分类对应关系，分页使用
//        'ISO标准' => 1,
//        'GB 国标' => 2,
//        '异形线' => 3,
//        '异形管' => 4,
//        'JIS标准（JIS Standard）' => 5,
//        '欧洲标准' => 6,
//        'ASTM标准' => 7,
//    ];

    private $prodcutCateIndexNew = [//产品二级分类对应关系，分页使用
        'ISO Standards' => [
            'pageIndex'=>1,
            'sons'=>[
                ['pageIndex'=>1,'name'=>'ISO 4954-Annex A'],
                ['pageIndex'=>2,'name'=>'ISO 4954-Annex B'],
                ['pageIndex'=>3,'name'=>'ISO 4954-Annex C'],
                ['pageIndex'=>4,'name'=>'ISO 4954-Annex D'],
            ]
            ],
        'GB 国标' => [
            'pageIndex'=>2,
            'sons'=>[
                ['pageIndex'=>1,'name'=>'GB/T 5953.1'],
                ['pageIndex'=>2,'name'=>'GB/T 5953.2'],
                ['pageIndex'=>3,'name'=>'GB/T 6983'],
            ]
        ],
        'JIS 標準' => [
            'pageIndex'=>3,
            'sons'=>[
                ['pageIndex'=>1,'name'=>'JIS G3505'],
                ['pageIndex'=>2,'name'=>'JIS G3507'],
                ['pageIndex'=>3,'name'=>'JIS G3508'],
                ['pageIndex'=>4,'name'=>'JIS G3509'],
                ['pageIndex'=>5,'name'=>'JIS G4107'],
                ['pageIndex'=>6,'name'=>'JIS G4051'],
                ['pageIndex'=>7,'name'=>'JIS G4053'],
                ['pageIndex'=>8,'name'=>'JIS G4804'],
                ['pageIndex'=>9,'name'=>'JIS G4805'],
            ]
        ],
        'ASTM Standards' => [
            'pageIndex'=>4,
            'sons'=>[
                ['pageIndex'=>1,'name'=>'ASTM 2282-18'],
            ]
        ],
        'Europäischen Standards' => [
            'pageIndex'=>5,
            'sons'=>[
                ['pageIndex'=>1,'name'=>'EN 10263-2'],
                ['pageIndex'=>2,'name'=>'EN 10263-3'],
                ['pageIndex'=>3,'name'=>'EN 10263-4'],
                ['pageIndex'=>4,'name'=>'EN 10263-5'],
            ]
        ],
        '异形线' => [
            'pageIndex'=>6,
            'sons'=>[
                ['pageIndex'=>1,'name'=>'汽车交通行业'],
                ['pageIndex'=>2,'name'=>'机械设备行业'],
                ['pageIndex'=>3,'name'=>'建筑建材行业'],
                ['pageIndex'=>4,'name'=>'其他行业'],
            ]
        ],
        '异形管' => [
            'pageIndex'=>7,
            'sons'=>[
                ['pageIndex'=>1,'name'=>'异形管'],
            ]
        ],
    ];
    private $langMap = [
        'iso105' => 'en',
        '国标' =>'zh',
        '异形管'=>'zh',
        '日本'=>'ja',
        '欧洲标准89'=>'de',
        '美国标准'=>'en',
    ];

    public function _initialize()
    {
        parent::_initialize();
        $this->view = new View();

        $this->lanArr = [
            'zh'=>['lan'=>'zh','field'=>'content_zh'],//中文
            'en'=>['lan'=>'en','field'=>'content_en'],//英语
            'de'=>['lan'=>'de','field'=>'content_ou'],//德语
            'ja'=>['lan'=>'ja','field'=>'content_rb'],//日语
            'ko'=>['lan'=>'ko','field'=>'content_hy'],//韩语
            'ru'=>['lan'=>'ru','field'=>'content_els'],//俄罗斯
            'es'=>['lan'=>'es','field'=>'content_es'],//西班牙语
            'pt'=>['lan'=>'pt','field'=>'content_pt'],//葡萄牙语
            'fr'=>['lan'=>'fr','field'=>'content_fr'],//法语
            'vi'=>['lan'=>'vi','field'=>''],//越南语
            'ar'=>['lan'=>'ar','field'=>''],//阿拉伯语
            'it'=>['lan'=>'it','field'=>''],//意大利语
            'tr'=>['lan'=>'tr','field'=>''],//土耳其语
            'pl'=>['lan'=>'pl','field'=>''],//波兰语
            'id'=>['lan'=>'id','field'=>''],//印尼语
            'th'=>['lan'=>'th','field'=>''],//泰语
            'ms'=>['lan'=>'ms','field'=>''],//马来语
        ];

        //模块和页面seo信息
        $pageModel = new \app\common\model\Page;
//        $marr = $pageModel->where('id','in',[26,27])->column('*','kname');
        $marr = $pageModel->column('*','kname');
        $this->moduleArr = $marr;
//        print_r($marr);die;

        //一版本的写死形式，单页面调试可以用下面这种
//        $this->moduleArr = [
////            'index'=>['page'=>'index'],//首页
////            'news'=>['page'=>'new'],//新闻
////            'medium'=>['page'=>'new-medium'],//媒体库
////            'about'=>['page'=>'about'],//关于我们-团队实力
////            'aboutdev'=>['page'=>'about-develop'],//关于我们-可持续发展
////            'aboutsec'=>['page'=>'about-secure'],//关于我们-安全第一
////            'aboutope'=>['page'=>'about-operate'],//关于我们-精细化运营
////            'aboutrecruit'=>['page'=>'about-recruit'],//关于我们-招聘中心
////            'aboutdown'=>['page'=>'about-down'],//关于我们-下载中心
////            'aboutpower'=>['page'=>'about-power'],//关于我们-能源设备
////            'quality'=>['page'=>'quality'],//质量保证-质量认证
////            'qualitydetection'=>['page'=>'quality-detection'],//质量保证-检测设备
////            'qualitymanufacture'=>['page'=>'quality-manufacture'],//质量保证-制程工艺
////            'qualityequipment'=>['page'=>'quality-equipment'],//质量保证-生产设备
////            'qualityservice'=>['page'=>'quality-service'],//质量保证-客户服务
////            'qualitypackage'=>['page'=>'quality-package'],//质量保证-包装标准
////            'qualitynew'=>['page'=>'quality-new'],//质量保证-新品研发
////            'contact'=>['page'=>'contact'],//联系我们-询盘表单提交
////            'contactmedia'=>['page'=>'contact-media'],//联系我们-社交媒体
////            'contactposition'=>['page'=>'contact-position'],//联系我们-公司位置
////            'composition'=>['page'=>'composition'],//集团组成
////            'center'=>['page'=>'center'],//产品中心
//            'detail'=>['page'=>'detail'],//产品详情
////            'product'=>['page'=>'product'],//产品应用
////            'pagestyle'=>['page'=>'page-style'],//产品应用详情
//        ];
    }

    public function buildHtmlCommon(){
        //构建网站siteMap
        $siteMapData = $this->getsiteMap();
        $todayDate = date('Y-m-d',time());
//        print_r($this->getSecCatesNew());die;
//        $module = $this->request->param('module','index');
        $lang = $this->request->param('lang','zh');//目标语言
        //原始语言规则
        $langMap = $this->langMap;
        foreach ($this->moduleArr as $module=>$moduleItem){
            $data = $this->getHtmlData($module);
            $data['apiurl'] = Env::get('app.apiurl');
            $data['title'] = $moduleItem['title']??'';
            $data['keywords'] = $moduleItem['keywords']??'';
            $data['description'] = $moduleItem['description']??'';
//            print_r($data);die;
            $modulePage = $moduleItem['page'];
            foreach ($this->lanArr as $lan=>$item){
                if($lang !== $lan && $lang != 'all'){
                    continue;
                }
                $data['fromLan'] = 'zh';//原语言
                $data['toLan'] = $item['lan'];//目标语言
                $data['modulePage'] = $modulePage;//分页使用-生成url
                $siteMapDomain = 'https://'.($data['toLan'] === 'zh'?'cn':$data['toLan']).'.chqwire.net/';//siteMap根据不同语言的域名
                if($module == 'center'){//产品列表页，二级分类+分页
                    $productModel = new \app\common\model\Product();
                    foreach ($data['cates'] as $rootCateName=>$rootCate) {
                        foreach ($rootCate['sons'] as $cate) {
                            $pagesize = 9;
                            $producPage = $productModel->field(['id', 'content_zh'])->where("content_zh->'$.category' like '%{$cate['name']}%'")->order(['createtime' => 'desc'])->paginate($pagesize);
//                            echo $productModel->getLastSql();die;
                            $modulePageCate = $modulePage . '-' . $rootCate['pageIndex'].'-'.$cate['pageIndex'];
                            //分页生成数据
                            $data['rootCateIndex'] = $rootCate['pageIndex'];
                            $data['cateSons'] = $rootCate['sons'];
                            $data['modulePage'] = $modulePageCate;
//                        print_r($producPage);die;
                            for ($page = 1; $page < $producPage->lastPage() + 1; $page++) {
                                $producsZh = [];
                                $producs = $productModel->field(['id', 'content', 'content_zh','language'])->where("content_zh->'$.category' like '%{$cate['name']}%'")->order(['createtime' => 'desc'])->paginate($pagesize, false, ['page' => $page]);
                                foreach ($producs as $produc) {
                                    //原始语言按产品语言来
                                    $data['language'] = $produc['language'];
                                    //产品原始语言
                                    if(isset($langMap[$produc['language']])){
                                        $data['fromLanPro'] = $langMap[$produc['language']];
                                    }

                                    $obj = json_decode($produc['content_zh'], 1);
//                                $obj['id'] = $produc['id'];

                                    $content = json_decode($produc['content'], 1);
//                                var_dump($content['product_name']);die;
                                    $pagePath = $this->getProductUrl(isset($content['product_name']) ? $content['product_name'] : null, $produc['id'],$data);
//                                if(isset($content['product_name'])){
//                                    $pagePath = str_replace(' ','-',$content['product_name']);//静态文件地址用产品名称
//                                }else{
//                                    $pagePath = 'product-'.$produc['id'];
//                                }
                                    $obj['pagePath'] = $pagePath;
                                    $obj['product_name'] = $content['product_name'];
                                    $producsZh[] = $obj;
                                }
//                            print_r($producsZh);die;
                                $data['products'] = $producsZh;
                                $data['pagelist'] = $producs;
                                $html = $this->view->fetch('cn/' . $modulePage, $data);
                                $htmlPage = $modulePageCate . '-' . $page . '.html';
                                $filePath = '../static/' . $item['lan'] . '/' . $htmlPage;
                                $dirName = pathinfo($filePath, PATHINFO_DIRNAME);
                                if (!is_dir($dirName)) {
                                    mkdir($dirName, 0755, true); // 第三个参数 true 表示可以创建多级目录
                                }
                                file_put_contents($filePath, $html);
                                //组装siteMap数据
                                $siteMapData.= $this->getsiteMapUrl($siteMapDomain.$htmlPage,$todayDate,'1');
                            }
                        }
                    }
                }else if($module == 'news'){//新闻列表页，二级分类+分页
                    $catesModel = new \app\common\model\Category();
                    $data['cates'] = $catesModel->where(['pid'=>17])->select();
                    $data['cates'][] = ['id'=>'1'];//表示所有的新闻
//                    print_r($data['cates']);die;
                    foreach ($data['cates'] as $k=>$cate) {
                        $isAll = $cate['id'] == 1;
                        $data['cateid'] = $cate['id'];
                        $modulePageCate = $modulePage . '-' .$cate['id'];
                        //分页生成数据
                        $data['modulePage'] = $isAll?$modulePage:$modulePageCate;
                        $newsModel = new \app\common\model\News();
                        $where = $isAll?[]:['category_id'=>$cate['id']];
                        $news = $newsModel->where($where)->order(['createtime' => 'desc'])->paginate($this->defaultPageSize);
                        //        print_r($news);die;
                        for ($page = 1; $page < $news->lastPage() + 1; $page++) {
                            $news = $newsModel->where($where)->order(['createtime' => 'desc'])->paginate($this->defaultPageSize, false, ['page' => $page]);
                            $data['news'] = $news;
                            $data['pagelist'] = $news;//$pagelist分页使用
                            $html = $this->view->fetch('cn/' . $modulePage, $data);
                            if($isAll){
                                $filePath = '../static/' . $item['lan'] . '/' . $modulePage . '-' . $page . '.html';
                            }else{
                                $filePath = '../static/' . $item['lan'] . '/'.$modulePageCate . '-' . $page . '.html';
                            }
                            $dirName = pathinfo($filePath, PATHINFO_DIRNAME);
                            if (!is_dir($dirName)) {
                                mkdir($dirName, 0755, true); // 第三个参数 true 表示可以创建多级目录
                            }
                            file_put_contents($filePath, $html);
                            //新闻详情页
                            foreach ($news as $p) {
                                $data['content'] = json_decode($p['content'], 1);
//                        print_r($data['content']);die;
                                $html = $this->view->fetch('cn/page-style', $data);
                                $htmlPage = 'news-detail-' . $p['id'] . '.html';
                                $filePath = '../static/' . $item['lan'] .'/'. $htmlPage;
                                $dirName = pathinfo($filePath, PATHINFO_DIRNAME);
                                if (!is_dir($dirName)) {
                                    mkdir($dirName, 0755, true); // 第三个参数 true 表示可以创建多级目录
                                }
                                file_put_contents($filePath, $html);
                                //组装siteMap数据
                                $siteMapData.= $this->getsiteMapUrl($siteMapDomain.$htmlPage,$todayDate,'0.5');
                            }
                        }
                    }
                }else if($module == 'aboutdown'){//下载中心列表页，二级分类+分页
                    $catesModel = new \app\common\model\Category();
                    $data['cates'] = $catesModel->where(['pid'=>15])->select();
                    array_unshift($data['cates'],['id'=>'1','name'=>'全部']);//表示所有的下载
//                    $data['cates'][] = ['id'=>'1','name'=>'全部'];//表示所有的下载
//                    print_r($data['cates']);die;
                    foreach ($data['cates'] as $k=>$cate) {
                        $isAll = $cate['id'] == 1;
                        $data['cateid'] = $cate['id'];
                        $modulePageCate = $modulePage . '-' .$cate['id'];
                        //分页生成数据
                        $data['modulePage'] = $isAll?$modulePage:$modulePageCate;

                        $downModel = new \app\common\model\Download();
                        $where = $isAll?[]:['category_id'=>$cate['id']];
                        $pagelist = $downModel->where($where)->order(['createtime' => 'desc'])->paginate($this->defaultPageSize);
                        //        print_r($news);die;
                        for ($page = 1; $page < $pagelist->lastPage() + 1; $page++) {
                            $lists = $downModel->where($where)->order(['createtime' => 'desc'])->paginate($this->defaultPageSize, false, ['page' => $page]);
                            $data['lists'] = $lists;
                            $data['pagelist'] = $pagelist;//$pagelist分页使用
                            $html = $this->view->fetch('cn/' . $modulePage, $data);
                            if($isAll){
                                $filePath = '../static/' . $item['lan'] . '/' . $modulePage . '-' . $page . '.html';
                            }else{
                                $filePath = '../static/' . $item['lan'] . '/'.$modulePageCate . '-' . $page . '.html';
                            }
                            $dirName = pathinfo($filePath, PATHINFO_DIRNAME);
                            if (!is_dir($dirName)) {
                                mkdir($dirName, 0755, true); // 第三个参数 true 表示可以创建多级目录
                            }
                            file_put_contents($filePath, $html);
                        }
                    }
                }else if($module == 'medium'){//媒体库列表页，二级分类+分页
                    $mediaModel = new \app\common\model\Media();
                    $pagelist = $mediaModel->order(['createtime'=>'desc'])->paginate($this->defaultPageSize);
                    for ($page = 1;$page<$pagelist->lastPage()+1;$page++){
                        $pagelist = $mediaModel->order(['createtime'=>'desc'])->paginate($this->defaultPageSize,false,['page'=>$page]);
                        $data['pagelist'] = $pagelist;//$pagelist分页使用
                        $html = $this->view->fetch('cn/'.$modulePage, $data);
                        $filePath = '../static/'.$item['lan'].'/'.$modulePage.'-'.$page.'.html';
                        $dirName = pathinfo($filePath, PATHINFO_DIRNAME);
                        if (!is_dir($dirName)) {
                            mkdir($dirName, 0755, true); // 第三个参数 true 表示可以创建多级目录
                        }
                        file_put_contents($filePath, $html);
                    }
                }else if($module == 'detail'){//产品详情
                    //Products & Service 换成 成卷和异形-下面的-Related Products-相关的 推荐到首页的 前4个 第一个图
                    $productModel = new \app\common\model\Product();
                    $productsJuan = $productModel->where('type','=',0)->where("content->'$.top'",'=',true)->limit(4)->select();
                    $productsYi = $productModel->where('type','=',1)->where("content->'$.top'",'=',true)->limit(4)->select();
                    //        var_dump($productsRelated);die;
                    $productBannersRelatedJuan = [];
                    foreach ($productsJuan as $product){
                        //原始语言按产品语言来
                        $data['language'] = $product['language'];
//                        $content_zh = json_decode($product['content_zh'],true);
//                        $content_zh['banner'][0]['title'] = $content_zh['product_name'];
                        //链接地址
                        $content = json_decode($product['content'],true);
//                        print_r($content);die;
                        $content['banner'][0]['title'] = $content['product_name'];

                        $pagePath = $this->getProductUrl(isset($content['product_name'])?$content['product_name']:null,$product['id'],$data);
//                        $content_zh['banner'][0]['url'] = '/'.$item['lan'].'/'.$pagePath.'.html';
                        $content['banner'][0]['url'] = '/'.$pagePath.'.html';
//                        $productBannersRelatedJuan[] = $content_zh['banner'][0];
                        $productBannersRelatedJuan[] = $content['banner'][0];
                    }
                    $productBannersRelatedYi = [];
                    foreach ($productsYi as $product){
                        //原始语言按产品语言来
                        $data['language'] = $product['language'];
//                        $content_zh = json_decode($product['content_zh'],true);
//                        $content_zh['banner'][0]['title'] = $content_zh['product_name'];
                        //链接地址
                        $content = json_decode($product['content'],true);
                        $content['banner'][0]['title'] = $content['product_name'];

                        $pagePath = $this->getProductUrl(isset($content['product_name'])?$content['product_name']:null,$product['id'],$data);
//                        $content_zh['banner'][0]['url'] = '/'.$item['lan'].'/'.$pagePath.'.html';
                        $content['banner'][0]['url'] = '/'.$pagePath.'.html';
//                        $productBannersRelatedYi[] = $content_zh['banner'][0];
                        $productBannersRelatedYi[] = $content['banner'][0];
                    }
                    $data['productBannersRelatedJuan'] = $productBannersRelatedJuan;
                    $data['productBannersRelatedYi'] = $productBannersRelatedYi;
//                    print_r($data['productBannersRelatedJuan']);die;

                    //产品遍历
                    $products = $productModel->select();
                    foreach ($products as $product){
                        //原始语言按产品语言来
                        $data['language'] = $product['language'];
                        if(isset($langMap[$product['language']])){
                            $data['fromLan'] = $langMap[$product['language']];
                        }
//                        $data['obj'] = json_decode($product['content_zh'],1);
                        $data['obj'] = $content = json_decode($product['content'],1);
//                        if(isset($content['product_name'])){
//                            $pagePath = str_replace(' ','-',$content['product_name']);//静态文件地址用产品名称
//                        }else{
//                            $pagePath = 'product-'.$produc['id'].'.html';
//                        }
                        $pagePath = $this->getProductUrl(isset($content['product_name'])?$content['product_name']:null,$product['id'],$data);

//                        print_r($product['content_zh']);die;
//                        $data['title'] = $data['obj']['title'];
//                        $data['keywords'] = $data['obj']['keywords'];
//                        $data['description'] = $data['obj']['description'];
//                        print_r($data);die;
                        //分类导航
                        $category = $data['obj']['category'];
                        $cateOne = $this->prodcutCateIndexNew[$category[1]];
                        $data['cateOne'] = $cateOne['pageIndex']??1;
                        $cateTwo = 1;
                        if($cateOne && !empty($cateOne['sons'])){
                            foreach ($cateOne['sons'] as $son){
                                if(isset($category[2]) && $son['name'] === $category[2]){
                                    $cateTwo = $son['pageIndex']??1;
                                }
                            }
                        }
                        $data['cateTwo'] = $cateTwo;

                        $html = $this->view->fetch('cn/'.$modulePage, $data);
                        $htmlPage = $pagePath.'.html';
                        $filePath = '../static/'.$item['lan'].'/'.$htmlPage;
                        $dirName = pathinfo($filePath, PATHINFO_DIRNAME);
                        if (!is_dir($dirName)) {
                            mkdir($dirName, 0755, true); // 第三个参数 true 表示可以创建多级目录
                        }
                        file_put_contents($filePath, $html);
                        //组装siteMap数据
                        $siteMapData.= $this->getsiteMapUrl($siteMapDomain.$htmlPage,$todayDate,'1');
//                        if($product['id'] != 1){
//                            die('/product-'.$product['id'].'.html');
//
//                        }
                    }
                }else if($module == 'pagestyle'){//样式单页数据
                    $tplModel = new \app\common\model\Tpl();
//                    $plist = $tplModel->where(['page'=>'51'])->select();//产品中心
                    $plist = $tplModel->select();//产品中心
                    foreach ($plist as $p){
                        $data['content'] = json_decode($p['content'],1);
//                        print_r($data['content']);die;
                        $html = $this->view->fetch('cn/'.$modulePage, $data);
                        $htmlPage = '/page-style-'.$p['id'].'.html';
                        $filePath = '../static/'.$item['lan'].$htmlPage;
                        $dirName = pathinfo($filePath, PATHINFO_DIRNAME);
                        if (!is_dir($dirName)) {
                            mkdir($dirName, 0755, true); // 第三个参数 true 表示可以创建多级目录
                        }
                        file_put_contents($filePath, $html);
                        //组装siteMap数据
                        $siteMapData.= $this->getsiteMapUrl($siteMapDomain.$htmlPage,$todayDate,'0.6');
                    }
                }else{//写死单页数据
                    $html = $this->view->fetch('cn/'.$modulePage, $data);
                    $filePath = '../static/'.$item['lan'].'/'.$modulePage.'.html';
                    $dirName = pathinfo($filePath, PATHINFO_DIRNAME);
                    if (!is_dir($dirName)) {
                        mkdir($dirName, 0755, true); // 第三个参数 true 表示可以创建多级目录
                    }
                    file_put_contents($filePath, $html);
                }

            }
        }
        $siteMapData = str_replace('https://cn.chqwire.net/',$siteMapDomain,$siteMapData);
        $siteMapData = str_replace('2020-10-05',$todayDate,$siteMapData);
        $siteMapData .= '</urlset>';
        //静态文件也转移过去
        $dirArr = ['css','js','img','fonts','utils'];
        if($lang != 'all'){
            foreach ($dirArr as $dir){
                $sourceDirectory = '../static/'.$dir;
                $destinationDirectory = APP_PATH.'../static/'.$lang.'/'.$dir;
                $this->recursive_copy($sourceDirectory, $destinationDirectory);
            }
            //网站siteMap
            file_put_contents(APP_PATH.'../static/'.$lang.'/sitemap.xml', $siteMapData);
        }else{
            foreach ($this->lanArr as $lan){
                foreach ($dirArr as $dir){
                    $sourceDirectory = '../static/'.$dir;
                    $destinationDirectory = APP_PATH.'../static/'.$lan['lan'].'/'.$dir;
                    $this->recursive_copy($sourceDirectory, $destinationDirectory);
                }
                //网站siteMap
                file_put_contents(APP_PATH.'../static/'.$lan['lan'].'/sitemap.xml', $siteMapData);
            }
        }
        die('done');
    }

    function recursive_copy($src, $dst)
    {
        $dir = opendir($src);
        if (!is_dir($dst)) {
//            echo $dst;
            mkdir($dst);
        }
        while (false !== ($file = readdir($dir))) {
            if (($file != '.') && ($file != '..')) {
                if (is_dir($src . '/' . $file)) {
                    $this->recursive_copy($src . '/' . $file, $dst . '/' . $file);
                } else {
                    copy($src . '/' . $file, $dst . '/' . $file);
                }
            }
        }
        closedir($dir);
    }

    public function getHtmlData($module){
        $data = [];
        switch ($module){
            case 'center':
                $data = ['cates'=>$this->getSecCatesNew()];
                break;
            case 'qualitynew':
                $tplModel = new \app\common\model\Tpl();
                $content = $tplModel->where(['page'=>'50'])->find()['content'];
                $data = ['content'=>json_decode($content,1)];
                break;
//            case 'aboutdown':
//                $downModel = new \app\common\model\Download();
//                $content = $downModel->select();
//                $data = ['content'=>$content];
//                break;
            case 'aboutpower':
                $tplModel = new \app\common\model\Tpl();
                $content = $tplModel->where(['page'=>'49'])->find()['content'];
                $data = ['content'=>json_decode($content,1)];
                break;
            case 'aboutrecruit':
                $tplModel = new \app\common\model\Tpl();
//                $obj = $tplModel->where(['page'=>'46'])->find();
//                $content = $obj['content'];
//                $data = ['content'=>json_decode($content,1),'id'=>$obj['id']];
                $list = $tplModel->where(['page'=>'46'])->select();
                $data = ['list'=>$list];
                break;
            case 'aboutdev':
                $tplModel = new \app\common\model\Tpl();
                $content = $tplModel->where(['page'=>'43'])->find()['content'];
                $data = ['content'=>json_decode($content,1)];
                break;
//            case 'medium':
//                $mediaModel = new \app\common\model\Media();
//                $pagelist = $mediaModel->order(['createtime'=>'desc'])->paginate($this->defaultPageSize);
//                $data = ['pagelist'=>$pagelist];
//                break;
//            case 'new':
//                $newsModel = new \app\common\model\News();
//                $news = $newsModel->order(['createtime'=>'desc'])->paginate($this->defaultPageSize);
//        //        print_r($news);die;
//                $data = ['news'=>$news,'pagelist'=>$news];//$pagelist分页使用
//                break;
            case 'contact':
                $data = ['apiurl'=>Env::get('app.apiurl', "")];
                break;
            case 'index':
                //产品-二级分类
//                $secCates = $this->getSecCates();
                $secCates = $this->getSecCatesNew();
                //产品-推荐到首页的 -成卷 4个 异形 4个 都用第一个图
                $productModel = new \app\common\model\Product();
                $productsJuan = $productModel->where('type','=',0)->where("content_zh->'$.top'",'=',true)->limit(4)->select();
                $productsYi = $productModel->where('type','=',1)->where("content_zh->'$.top'",'=',true)->limit(4)->select();
                $products = array_merge($productsJuan,$productsYi);
                //        var_dump($products);die;
                $productBanners = [];//首页banner
                $productDescs = [];//首页产品应用
                foreach ($products as $product){
                    //原始语言按产品语言来
                    $data['language'] = $product['language'];

                    $content = json_decode($product['content_zh'],true);
                    $content_en = json_decode($product['content'],true);
//            print_r($content);die;
//                    $productBanners = array_merge($productBanners,$content['banner']);
                    $pagePath = $this->getProductUrl(isset($content_en['product_name'])?$content_en['product_name']:null,$product['id'],$data);
                    $content['banner'][0]['url'] = $pagePath.'.html';
                    $productBanners[] = $content['banner'][0];
                    $desc = ['title'=>$content['product_name'],'desc'=>$content['detail']['des']];
                    $productDescs[] =$desc;
                }
//        print_r($productDescs);die;
                //新闻
                $newsModel = new \app\common\model\News();
                $news = $newsModel->order(['createtime'=>'desc'])->limit(5)->select();
                //下载中心
                $downModel = new \app\common\model\Download();
                $download = $downModel->order(['createtime'=>'desc'])->limit(1)->select();
                //媒体库
                $mediaModel = new \app\common\model\Media();
                $media = $mediaModel->order(['createtime'=>'desc'])->limit(1)->select();
                //招聘中心
                $tplModel = new \app\common\model\Tpl();
                $content = $tplModel->where(['page'=>'46'])->find()['content'];
                $zpdata = json_decode($content,1);
                //产品应用
                $plist = $tplModel->where(['page'=>'51'])->select();
                //新品研发
                $nlist = $tplModel->where(['page'=>'50'])->select();
                $data = ['cates'=>$secCates,
                    'productBanners'=>$productBanners,
                    'productDescs'=>$productDescs,
                    'news'=>$news,
                    'download'=>$download,
                    'media'=>$media,
                    'zpdata'=>$zpdata,
                    'plist'=>$plist,
                    'nlist'=>$nlist,
                ];
                break;
        }
        return$data;
    }

    /**
     * 构造访问的产品URL
     * @param $product_name
     * @param $id
     * @param array $data
     * @return array|string|string[]
     */
    private function getProductUrl($product_name,$id,$data=[]){
//        var_dump($product_name);
//        var_dump($id);
        $pagePath='';
        if($product_name){
            //原始语言的文件名字换为目标语言的名字
            if(in_array($data['language'],['国标','异形管'])){
                $product_name = translate($product_name,'zh','en');
            }else if(in_array($data['language'],['日本'])){
                $product_name = translate($product_name,'ja','en');
            }else if(in_array($data['language'],['欧洲标准89'])){
                $product_name = translate($product_name,'de','en');
            }

            $pagePath = str_replace(' ','-',$product_name);//静态文件地址用产品名称
        }else{
            $pagePath = 'product-'.$id;
        }
        return $pagePath;
    }
    private function getSecCates(){
        //产品分类：["线圈", "ISO标准", "ISO 4954-附录A"]  取二级分类
        $productModel = new \app\common\model\Product();
        $catesArr = $productModel->query("SELECT DISTINCT(content_zh->'$.category') AS category from fa_zz_product");
        $cates = [];
        foreach ($catesArr as $item){
            if($item && $item['category']){
                $cates[] = json_decode($item['category'],1)[1];
            }
        }
        $cates = array_values(array_unique($cates));
        $finalCates = [];
        foreach ($cates as $cate){
            $finalCates[] = ['name'=>$cate,'pageItem'=>$this->prodcutCateIndex[$cate]];
        }
        return $finalCates;
    }

    private function getSecCatesNew(){
        return $this->prodcutCateIndexNew;
//        //产品分类：["线圈", "ISO标准", "ISO 4954-附录A"]  获取二级+三级分类
//        $productModel = new \app\common\model\Product();
//        $catesArr = $productModel->query("SELECT DISTINCT(content_zh->'$.category') AS category from fa_zz_product");
//        $cates = [];
//        foreach ($catesArr as $item){
//            if($item && $item['category']){
//                $ca =  json_decode($item['category'],1);
//                $cates[$ca[1]][] =$ca[2];
//            }
//        }
//        print_r($cates);die;
////        $cates = array_values(array_unique($cates));
//        $finalCates = [];
//        foreach ($cates as $cate){
//            $finalCates[] = ['name'=>$cate,'pageItem'=>$this->prodcutCateIndex[$cate]];
//        }
//        return $finalCates;
    }

    public function buildNewsHtml()
    {
        //新闻
        $newsModel = new \app\common\model\News();
        $news = $newsModel->order(['createtime'=>'desc'])->paginate(1);
//        print_r($news);die;
        $data = ['news'=>$news,'obj'=>$news];//obj分页使用
        foreach ($this->lanArr as $lan=>$item){
            $data['fromLan'] = 'zh';//原语言
            $data['toLan'] = $item['lan'];//目标语言
            $html = $this->view->fetch('cn/new', $data);
            $filePath = '../static/'.$item['lan'].'/new.html';
            $dirName = pathinfo($filePath, PATHINFO_DIRNAME);
            if (!is_dir($dirName)) {
                mkdir($dirName, 0755, true); // 第三个参数 true 表示可以创建多级目录
            }
            file_put_contents($filePath, $html);
        }

        die('done');
    }

    public function buildIndexHtml()
    {
        //产品-推荐到首页的
        $productModel = new \app\common\model\Product();
        $products = $productModel->where("content_zh->'$.top'",'=',true)->limit(4)->select();
//        var_dump($products);die;
        $productBanners = [];//首页banner
        $productDescs = [];//首页产品应用
        foreach ($products as $product){
            $content = json_decode($product['content_zh'],true);
//            print_r($content);die;
            $productBanners = array_merge($productBanners,$content['banner']);
            $desc = ['title'=>$content['product_name'],'desc'=>$content['detail']['des']];
            $productDescs[] =$desc;
        }
//        print_r($productDescs);die;
        //新闻
        $newsModel = new \app\common\model\News();
        $news = $newsModel->order(['createtime'=>'desc'])->limit(5)->select();
        $data = ['productBanners'=>$productBanners,'productDescs'=>$productDescs,'news'=>$news];
        // 实例化视图类，并指定模板文件和输出文件
//        $view = new View();
        foreach ($this->lanArr as $lan=>$item){
            $data['fromLan'] = 'zh';//原语言
            $data['toLan'] = $item['lan'];//目标语言
            $html = $this->view->fetch('cn/index', $data);
            $filePath = '../static/'.$item['lan'].'/index.html';
            $dirName = pathinfo($filePath, PATHINFO_DIRNAME);
            if (!is_dir($dirName)) {
                mkdir($dirName, 0755, true); // 第三个参数 true 表示可以创建多级目录
            }
            file_put_contents($filePath, $html);
        }

        die('done');
    }

    public function buildHtml($htmlfile = '', $data = ['errInfo'=>[]])
    {
        // 实例化视图类，并指定模板文件和输出文件
        $view = new View();
        $html = $view->fetch('test/install', $data);
        // 输出到文件
        // 确保目录存在
        $filePath = './static/html/index.html';
        $dirName = pathinfo($filePath, PATHINFO_DIRNAME);
        if (!is_dir($dirName)) {
            mkdir($dirName, 0755, true); // 第三个参数 true 表示可以创建多级目录
        }
        file_put_contents($filePath, $html);
    }

    public function getsiteMapUrl($url,$lastmod,$pri){
        return '<url>
    <loc>'.$url.'</loc>
    <lastmod>'.$lastmod.'</lastmod>
    <changefreq>daily</changefreq>
    <priority>'.$pri.'</priority>
  </url>'."\r\n";
    }
    public function getsiteMap(){
        return '<urlset xmlns="http://www.google.com/schemas/sitemap/0.9">
  <url>
    <loc>https://cn.chqwire.net/index.html</loc>
    <lastmod>2020-10-05</lastmod>
    <changefreq>daily</changefreq>
    <priority>1</priority>
  </url>
  <url>
    <loc>https://cn.chqwire.net/about-develop.html</loc>
    <lastmod>2020-10-05</lastmod>
    <changefreq>daily</changefreq>
    <priority>0.8</priority>
  </url>
  <url>
    <loc>https://cn.chqwire.net/about-operate.html</loc>
    <lastmod>2020-10-05</lastmod>
    <changefreq>daily</changefreq>
    <priority>0.7</priority>
  </url>
  <url>
    <loc>https://cn.chqwire.net/product.html</loc>
    <lastmod>2020-10-05</lastmod>
    <changefreq>daily</changefreq>
    <priority>0.7</priority>
  </url>
  <url>
    <loc>https://cn.chqwire.net/about.html</loc>
    <lastmod>2020-10-05</lastmod>
    <changefreq>daily</changefreq>
    <priority>0.6</priority>
  </url>
  <url>
    <loc>https://cn.chqwire.net/about-power.htmll</loc>
    <lastmod>2020-10-05</lastmod>
    <changefreq>daily</changefreq>
    <priority>0.6</priority>
  </url>
  <url>
    <loc>https://cn.chqwire.net/about-secure.html</loc>
    <lastmod>2020-10-05</lastmod>
    <changefreq>daily</changefreq>
    <priority>0.5</priority>
  </url>
  <url>
    <loc>https://cn.chqwire.net/about-recruit.html</loc>
    <lastmod>2020-10-05</lastmod>
    <changefreq>daily</changefreq>
    <priority>0.4</priority>
  </url>
  <url>
    <loc>https://cn.chqwire.net/about-down-1.html</loc>
    <lastmod>2020-10-05</lastmod>
    <changefreq>daily</changefreq>
    <priority>0.4</priority>
  </url>
  <url>
    <loc>https://cn.chqwire.net/quality.html</loc>
    <lastmod>2020-10-05</lastmod>
    <changefreq>daily</changefreq>
    <priority>0.8</priority>
  </url>
  <url>
    <loc>https://cn.chqwire.net/quality-detection.html</loc>
    <lastmod>2020-10-05</lastmod>
    <changefreq>daily</changefreq>
    <priority>0.8</priority>
  </url>
  <url>
    <loc>https://cn.chqwire.net/quality-manufacture.html</loc>
    <lastmod>2020-10-05</lastmod>
    <changefreq>daily</changefreq>
    <priority>0.6</priority>
  </url>
  <url>
    <loc>https://cn.chqwire.net/quality-equipment.html</loc>
    <lastmod>2020-10-05</lastmod>
    <changefreq>daily</changefreq>
    <priority>0.8</priority>
  </url>
  <url>
    <loc>https://cn.chqwire.net/quality-service.html</loc>
    <lastmod>2020-10-05</lastmod>
    <changefreq>daily</changefreq>
    <priority>0.6</priority>
  </url>
  <url>
    <loc>https://cn.chqwire.net/quality-package.html</loc>
    <lastmod>2020-10-05</lastmod>
    <changefreq>daily</changefreq>
    <priority>0.6</priority>
  </url>
  <url>
    <loc>https://cn.chqwire.net/quality-new.html</loc>
    <lastmod>2020-10-05</lastmod>
    <changefreq>daily</changefreq>
    <priority>0.6</priority>
  </url>
  <url>
    <loc>https://cn.chqwire.net/new-1.html</loc>
    <lastmod>2020-10-05</lastmod>
    <changefreq>daily</changefreq>
    <priority>0.5</priority>
  </url>
  <url>
    <loc>https://cn.chqwire.net/new-medium-1.html</loc>
    <lastmod>2020-10-05</lastmod>
    <changefreq>daily</changefreq>
    <priority>0.5</priority>
  </url>
  <url>
    <loc>https://cn.chqwire.net/contact.html</loc>
    <lastmod>2020-10-05</lastmod>
    <changefreq>daily</changefreq>
    <priority>0.5</priority>
  </url>
  <url>
    <loc>https://cn.chqwire.net/contact-media.html</loc>
    <lastmod>2020-10-05</lastmod>
    <changefreq>daily</changefreq>
    <priority>0.5</priority>
  </url>
  <url>
    <loc>https://cn.chqwire.net/contact-position.html</loc>
    <lastmod>2020-10-05</lastmod>
    <changefreq>daily</changefreq>
    <priority>0.5</priority>
  </url>
   <url>
    <loc>https://cn.chqwire.net/composition.html</loc>
    <lastmod>2020-10-05</lastmod>
    <changefreq>daily</changefreq>
    <priority>0.5</priority>
  </url>
';
    }
}